package com.hiwan.system.common.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hiwan.platform.util.StackTraceUtil;
import com.hiwan.system.util.ConfigUtil;

/**
 * 
 * 系统清理机制
 * 
 * @author WQ
 *
 */
public class HiwanFrameworkSysClearThread extends Thread {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public void run() {
		long sysRubbishClearStep = Long.valueOf(ConfigUtil.getConfig("sysrubbishclear.step"));
		while (true) {
			sysRubbishClear();
			try {
				Thread.sleep(sysRubbishClearStep * 60 * 1000);
			} catch (InterruptedException e) {
				logger.error(StackTraceUtil.getStackTrace(e));
			}
		}
	}

	/**
	 * 垃圾清理机制
	 */
	public synchronized void sysRubbishClear() {
		Runtime runtime = Runtime.getRuntime();
		logger.info(
				"----HiwanFramework Before SysRubbishClear (maxMemory:{}, totalMemory: {} KB,freeMemory: {} KB) ----",
				new Object[] { runtime.maxMemory(), runtime.totalMemory() / 1024, runtime.freeMemory() });
		runtime.gc();
		logger.info("----HiwanFramework After SysRubbishClear (maxMemory:{}, totalMemory:{} KB, freeMemory:{} KB) ----",
				new Object[] { runtime.maxMemory(), runtime.totalMemory() / 1024, runtime.freeMemory() });
	}

}
